﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using libs.Model;
using libs.DAL;
using libs.DBUtility;

namespace Sever
{
    /// <summary>
    /// ParameterHandler 的摘要说明
    /// </summary>
    public class ParameterHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["type"];
            int qid = int.Parse(context.Request["id"]);
            userDao userd = new userDao();
            if (!userd.Exists(qid))
            {
                context.Response.Write("{\"result\":\"error\"}");
            }
            else
            {
                user uuu = userd.GetModel("id=" + qid);
                if (uuu.enable == false)
                {
                    context.Response.Write("{\"result\":\"error\"}");
                }
                else
                {
                    parameterDao dao = new parameterDao();
                    if (!string.IsNullOrEmpty(type))
                    {
                        switch (type)
                        {
                                //最新的一条信息
                            case "LastInfo":
                                parameter last = dao.GetModelLast("order by convert(datetime,time) desc");
                                string lastjson = JsonHelper.SerializeObject(last);
                                context.Response.Write("{\"result\":" + lastjson + "}");
                                break;
                            //分类别当天的全部信息
                            case "AllInfo":
                                string timeall = context.Request["time"];
                                List<parameter> listall = dao.GetList("time like '" + timeall + "%' order by convert(datetime,time) asc");
                                string listjson = JsonHelper.SerializeObject(listall);
                                context.Response.Write("{\"result\":" + listjson + "}");
                                break;
                        }
                    }
                }
            }
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}